<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class country_name_translation
{
	public static $source_type = array(
		'blu_ray' => 1,
		'cinema' => 2,
		'dvd' => 3,
		'film_festival' => 4,
		'tv' => 5,
	);
	public static $compress_type = array(
		'iso' => 1,
		'remux' => 2,
		'rip' => 3,
	);
	public static $resolution_type = array(
		'1080p'	=> '1',
		'720p' => '2',
		'480p' => '3',
	);
	public static $protocol = array(
		'bt'	=> '1',
		'ed2k' => '2',
		'ftp3' => '3',
		'http' => '4',
	);
	private $db;
	private $table_name = 'country_name_translation';
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		if(isset($data['compress_type'])&&!empty($data['compress_type']))
		{
			$data_for_query['compress_type'] = intval($data['compress_type']);
		}
		if(isset($data['file_name'])&&!empty($data['file_name']))
		{
			$data_for_query['file_name'] = trim($data['file_name']);
		}
		if(isset($data['file_size'])&&!empty($data['file_size']))
		{
			$data_for_query['file_size'] = intval($data['file_size']);
		}
		if(!isset($data['file_url'])||empty($data['file_url']))
		{
			return false;
		}
		$data_for_query['file_url'] = trim($data['file_url']);
		if(!isset($data['film_id'])||empty($data['film_id']))
		{
			return false;
		}
		$data_for_query['film_id'] = trim($data['film_id']);
		if(isset($data['media_info'])&&!empty($data['media_info']))
		{
			$data_for_query['media_info'] = trim($data['media_info']);
		}
		if(isset($data['promulgator_name'])&&!empty($data['promulgator_name']))
		{
			$data_for_query['promulgator_name'] = trim($data['promulgator_name']);
		}
		if(isset($data['protocol'])&&!empty($data['protocol']))
		{
			$data_for_query['protocol'] = intval($data['protocol']);
		}
		if(isset($data['resolution_height'])&&!empty($data['resolution_height']))
		{
			$data_for_query['resolution_height'] = intval($data['resolution_height']);
		}
		if(isset($data['resolution_type'])&&!empty($data['resolution_type']))
		{
			$data_for_query['resolution_type'] = intval($data['resolution_type']);
		}
		if(isset($data['resolution_width'])&&!empty($data['resolution_width']))
		{
			$data_for_query['resolution_width'] = trim($data['resolution_width']);
		}
		if(isset($data['ripper_email'])&&!empty($data['ripper_email']))
		{
			$data_for_query['ripper_email'] = intval($data['ripper_email']);
		}
		if(isset($data['ripper_name'])&&!empty($data['ripper_name']))
		{
			$data_for_query['ripper_name'] = trim($data['ripper_name']);
		}
		if(isset($data['ripper_url'])&&!empty($data['ripper_url']))
		{
			$data_for_query['ripper_url'] = intval($data['ripper_url']);
		}
		if(isset($data['runtime'])&&!empty($data['runtime']))
		{
			$data_for_query['runtime'] = intval($data['runtime']);
		}
		if(isset($data['spoken_language_id_sum'])&&!empty($data['spoken_language_id_sum']))
		{
			$data_for_query['spoken_language_id_sum'] = trim($data['spoken_language_id_sum']);
		}
		if(isset($data['source_type'])&&!empty($data['source_type']))
		{
			$data_for_query['source_type'] = trim($data['source_type']);
		}
		if(!isset($data['source_id'])||empty($data['source_id']))
		{
			return false;
		}
		$data_for_query['source_id'] = trim($data['source_id']);
		if(isset($data['video_bitrate'])&&!empty($data['video_bitrate']))
		{
			$data_for_query['video_bitrate'] = trim($data['video_bitrate']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$count = $this->db->query($sql);
		if(empty($count))
		{
			//throw new Exception("faild");
			throw new Exception("");
		}
		else
		{
			return $count;
		}
	}
	
	public function select($where,$column='*')
	{
		$sql_1 = "SELECT ".$this->db->implodeToColumn($column)." FROM ".$this->table_name." WHERE ".$this->db->implodeToWhere($where);
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if($fetch_result==false)
		{
			//throw new Exception("faild");
			throw new Exception("");
		}
		else
		{
			return $fetch_result;
		}
	}
	
	public function getNameByCountryIdAndTag($country_id,$tag='zh-Hans-CN')
	{
		$where = array('country_id' => $country_id);
		$column = $tag;
		$result = $this->select($where,$column);
		return $result[0][$tag];
	}
}

?>
